1703, American English, from Dutch koekje "little cake," diminutive of koek "cake," from Middle Dutch koke (see cake (n.)). Slang application to persons attested since 1920. Phrase that's the way the cookie crumbles "that's the way things happen" is from 1957.
To behave properly; KEEP one's NOSE CLEAN: If he doesn't toe the mark, fire him
[entry form 1813+, variant 1895+; fr the mark or line indicating the starting point of a race]
gripe one's ass, that's the way the ball bounces, tough cookie